package com.incar.chuxing.activity;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.incar.chuxing.R;
import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlConstant;
import com.incar.chuxing.base.BaseActivity;
import com.incar.chuxing.bean.CarOrderDetailBean;
import com.incar.chuxing.bean.ParkFeeBean;
import com.incar.chuxing.utils.Dialogs.CarParkExceptionDialog;
import com.incar.chuxing.utils.Dialogs.CarPayParkFeeDialog;
import com.incar.chuxing.utils.Dialogs.InCarDialog;
import com.incar.chuxing.utils.IntentKey;
import com.incar.chuxing.utils.MyApplication;
import com.incar.chuxing.utils.SharedPreferencesUtils;
import com.incar.chuxing.utils.Toast.ToastCompat;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CarParkingFeeActivity extends BaseActivity implements View.OnClickListener {
    @BindView(R.id.detail_order_toolbar_car)
    RelativeLayout detailOrderToolbarCar;
    @BindView(R.id.title_name)
    TextView titleName;
    @BindView(R.id.title_tv_more)
    TextView title_tv_more;
    @BindView(R.id.title_more)
    ImageView title_more;
    @BindView(R.id.title_back)
    ImageView title_back;
    @BindView(R.id.iv_car)
    ImageView ivCar;
    @BindView(R.id.tv_carNum)
    TextView tvCarNum;
    @BindView(R.id.tv_km)
    TextView tvKm;
    @BindView(R.id.tv_seats)
    TextView tvSeats;
    @BindView(R.id.edt_fee)
    EditText edtFee;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    @BindView(R.id.tv_nofee)
    TextView tvNofee;
    private Context mContext = this;
    private String orderId;
    private String noFeeValue;

    @Override
    public int getLayoutId() {
        return R.layout.activity_car_parkfee;
    }

    @Override
    public void initView() {
        title_more.setVisibility(View.GONE);
        title_tv_more.setVisibility(View.VISIBLE);
        title_tv_more.setText("联系客服");
        titleName.setText("停车费补偿");
        orderId = getIntent().getStringExtra(IntentKey.INTENT_ORDER_ID);

        getCarOrderDetail();
    }

    @Override
    public void initData() {
        super.initData();
        tvCommit.setOnClickListener(this);
        tvNofee.setOnClickListener(this);
        title_tv_more.setOnClickListener(this);
        title_back.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                onBackPressed();
                break;
            case R.id.tv_commit:
                if (TextUtils.isEmpty(edtFee.getText().toString())) {
                    ToastCompat.makeText(mContext, "请输入您的停车费用", Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                } else {
                    getParkFee(orderId, (Integer.parseInt(edtFee.getText().toString())*100)+"");
                }
                break;
            case R.id.tv_nofee:
                noFeeValue = "0";
                getParkFee(orderId, noFeeValue);
                break;
            case R.id.title_tv_more:
                final InCarDialog inCarDialog2 = new InCarDialog(mContext);
                inCarDialog2.setTitle(getString(R.string.dialogTitle));
                inCarDialog2.setMessage("是否拨打客服电话" + ((MyApplication) getApplication()).getHotLine());
                inCarDialog2.setYesOnclickListener("确定", new InCarDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        Intent intent = new Intent();
                        intent.setAction(Intent.ACTION_DIAL);
                        intent.setData(Uri.parse("tel:" + ((MyApplication) getApplication()).getHotLine()));
                        startActivity(intent);
                        inCarDialog2.dismiss();
                    }
                });
                inCarDialog2.setNoOnclickListener("取消", new InCarDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        inCarDialog2.dismiss();
                    }
                });
                inCarDialog2.show();
                break;
        }
    }


    private void showExceptionDialog() {
        final CarParkExceptionDialog exceptionDialog = new CarParkExceptionDialog(this);
        exceptionDialog.setMessage("停车费用异常，详情请咨询客服。");
        exceptionDialog.setYesOnclickListener("呼叫", new CarParkExceptionDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_DIAL);
                intent.setData(Uri.parse("tel:" + ((MyApplication) getApplication()).getHotLine()));
                startActivity(intent);
                exceptionDialog.dismiss();
            }
        });
        exceptionDialog.setNoOnclickListener("取消", new CarParkExceptionDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                exceptionDialog.dismiss();
            }
        });
        exceptionDialog.show();
    }

    private void showPayDialog() {
        final CarPayParkFeeDialog payDialog = new CarPayParkFeeDialog(this);
        payDialog.setMoney("￥" + edtFee.getText().toString());
        payDialog.setCancelable(false);
        payDialog.show();
        new Handler(new Handler.Callback() {
            //处理接收到的消息的方法
            @Override
            public boolean handleMessage(Message arg0) {
                //实现页面跳转
                //跳转支付界面
                Intent intent = new Intent(mContext, PaymentActivity.class);
                intent.putExtra(IntentKey.INTENT_ORDER_ID, orderId);
                startActivity(intent);
                if (payDialog != null) {
                    payDialog.dismiss();
                }
                return false;
            }
        }).sendEmptyMessageDelayed(0, 2500); //表示延时三秒进行任务的执行
    }


    private void getParkFee(final String orderId, String parkingFee) {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getParkFeeData(((MyApplication) getApplication()).getRequestFieldMap(), orderId, parkingFee).enqueue(new Callback<ParkFeeBean>() {
            @Override
            public void onResponse(Call<ParkFeeBean> call, Response<ParkFeeBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        if ("0".equals(noFeeValue)) {
                            Intent intent = new Intent(mContext, PaymentActivity.class);
                            intent.putExtra(IntentKey.INTENT_ORDER_ID, orderId);
                            startActivity(intent);
                        } else {
                            showPayDialog();
                        }
                    } else {
                        showExceptionDialog();
                        // ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<ParkFeeBean> call, Throwable t) {

                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                Log.i("neterror", "004" + t.toString());
            }
        });
    }

    private void getCarOrderDetail() {
        ApiStore.getInstance().getApiService(UrlConstant.Request.BASE_URL).getTravelDetail(((MyApplication) getApplication()).getRequestFieldMap(), orderId, "1").enqueue(new Callback<CarOrderDetailBean>() {
            @Override
            public void onResponse(Call<CarOrderDetailBean> call, Response<CarOrderDetailBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        CarOrderDetailBean item = response.body();
                        tvSeats.setText(item.getAttachment().getOrderFavourableModel().getCarModel().getSeat() + "座");
                        tvCarNum.setText(item.getAttachment().getOrderFavourableModel().getCarModel().getLicenseTag());
                        tvKm.setText("剩余里程："+item.getAttachment().getOrderFavourableModel().getCarModel().getMileage() + "公里");
                        Glide.with(mContext).load(((MyApplication) getApplication()).getImageUrl() + item.getAttachment().getOrderFavourableModel().getCarModel().getPicture()).into(ivCar);
                    } else {
                        ToastCompat.makeText(mContext, response.body().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            }

            @Override
            public void onFailure(Call<CarOrderDetailBean> call, Throwable t) {

                ToastCompat.makeText(mContext, getResources().getString(R.string.badNetwork), Toast.LENGTH_LONG).setGravity(Gravity.CENTER, 0, 0).show();
                Log.i("neterror", "004" + t.toString());
            }
        });
    }

    @Override
    public void onBackPressed() {
        final InCarDialog inCarDialog = new InCarDialog(this);
        inCarDialog.setTitle(getString(R.string.dialogTitle));
        inCarDialog.setMessage("您确认暂不支付？");
        inCarDialog.setYesOnclickListener("继续支付", new InCarDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                inCarDialog.dismiss();
            }
        });
        inCarDialog.setNoOnclickListener("暂不支付", new InCarDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                SharedPreferencesUtils.getInstance(mContext).putIntValue("unpay", 1);
                Intent intent = new Intent(mContext, MainActivity.class);
                startActivity(intent);
                inCarDialog.dismiss();
            }
        });
        inCarDialog.show();
    }
}